//% color="#00c4dd" iconWidth=50 iconHeight=40
namespace UNIHIKER_SHIELD{
    //% block="红外发送、接收" blockType="tag"
    export function tagtest1() {}
    //% block="IR send msg[msg]" blockType="command"  
    //% msg.shadow="normal" msg.defl="0xfd807f"
    export function IRSend(parameter: any, block: any) { 
        let msg=parameter.msg.code;
		
		// 添加必要的导入
		Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
		Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		
		// 初始化开发
		Generator.addInit(`board_begin`, `Board().begin()`, { once: true });
		Generator.addInit(`init_SHIELD`, `shield = SHIELD()`, { once: true });
		Generator.addInit(`init_ir`, `ir = shield.ir`, { once: true });

        // 直接发送十六进制整数
        Generator.addCode(`ir.send(${msg})`); 		
    }


	export function IRRecv(parameter: any, block: any) { 
		let fun = parameter.fun.code; // 获取回调函数名
		
		// 添加必要的导入
		Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
		Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		
		// 初始化开发
		Generator.addInit(`board_begin`, `Board().begin()`, { once: true });
		Generator.addInit(`init_SHIELD`, `shield = SHIELD()`, { once: true });
		Generator.addInit(`init_ir`, `ir = shield.ir`, { once: true });
		
		// 注册回调函数
		Generator.addCode(`ir.set_receive_callback(${fun})`); 

	}


	//% block="when IR receives data call [fun]" blockType="hat"
	//% fun.shadow="normal" fun.defl="ir_callback"
	export function EventIRRecv(parameter: any, block: any) {
		let fun = parameter.fun.code;
		
		// 添加必要的导入
		Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
		Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		
		// 初始化开发
		Generator.addInit(`board_begin`, `Board().begin()`, { once: true });
		Generator.addInit(`init_SHIELD`, `shield = SHIELD()`, { once: true });
		Generator.addInit(`init_ir`, `ir = shield.ir`, { once: true });
		
		// 注册回调函数
		Generator.addEvent(`ir_receive_event`, `${fun}`, `data`, true);
		Generator.addInit(`register_ir_callback`, `ir.set_receive_callback(${fun})`);
	}


    //% block="IR Recv data" blockType="reporter"  
    export function GetIRRecv(parameter: any, block: any) { 
		
		// 添加必要的导入
		Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
		Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		
		// 初始化开发
		Generator.addInit(`board_begin`, `Board().begin()`, { once: true });
		Generator.addInit(`init_SHIELD`, `shield = SHIELD()`, { once: true });
		Generator.addInit(`init_ir`, `ir = shield.ir`, { once: true });
		
		Generator.addCode(`data`)		
    }

    //% block="WS2812 RGB灯" blockType="tag"
    export function tagtest2() {}	




    //% block="RGB lights Set [BRIGHT]" blockType="command"  
    //% BRIGHT.shadow="range" BRIGHT.params.min=0 BRIGHT.params.max=255 BRIGHT.defl=128
    export function RGBbrightness(parameter: any, block: any) { 
        let bright=parameter.BRIGHT.code;
        // Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        // Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		// Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
        Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P13)),3)`);		
		Generator.addCode(`RGB.brightness(${bright})`)  
    }


    //% block="All RGB lights are off" blockType="command"  
    export function RGBoff(parameter: any, block: any) { 
        Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
		Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P13)),3)`);
		Generator.addCode(`RGB.clear()`)  
    }


    //% block="Set the [COLOR] of the [NUMBER] light" blockType="command"  
    //% NUMBER.shadow="dropdown" NUMBER.options="NUMBER"
    //% COLOR.shadow="colorPalette"
    export function RGBcolor1(parameter: any, block: any) { 
        let number=parameter.NUMBER.code;
        let color=parameter.COLOR.code; //获取传进来的值
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
		Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P13)),3)`);
		Generator.addCode(`RGB[${number}] = (${r},${g},${b})`)  
    }


    //% block="RGB lights [NUMBER1] to [NUMBER2] display [COLOR]" blockType="command"  
    //% NUMBER1.shadow="dropdown" NUMBER1.options="NUMBER"
    //% NUMBER2.shadow="dropdown" NUMBER2.options="NUMBER"
    //% COLOR.shadow="colorPalette"
    export function RGBcolor2(parameter: any, block: any) { 
        let number1=parameter.NUMBER1.code;
        let number2=parameter.NUMBER2.code;
        let color=parameter.COLOR.code; 
        Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
		Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P13)),3)`);
		Generator.addCode(`RGB.range_color(${number1},${number2},${color})`)   
    }



    //% block="RGB lights [NUMBER1] to [NUMBER2] display gradient color. tone from [RANGE1] to [RANGE2]" blockType="command"  
    //% NUMBER1.shadow="dropdown" NUMBER1.options="NUMBER"
    //% NUMBER2.shadow="dropdown" NUMBER2.options="NUMBER"
    //% RANGE1.shadow="range" RANGE1.params.min=0 RANGE1.params.max=360 RANGE1.defl=1
    //% RANGE2.shadow="range" RANGE2.params.min=0 RANGE2.params.max=360 RANGE2.defl=360
    export function RGBtone(parameter: any, block: any) { 
        let number1=parameter.NUMBER1.code;
        let number2=parameter.NUMBER2.code;
        let range1=parameter.RANGE1.code; 
        let range2=parameter.RANGE2.code; 
        Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
		Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P13)),3)`);
		Generator.addCode(`RGB.rainbow(${number1},${number2},${range1},${range2})`)   
    }


    //% block="RGB light moves [NUM] unit" blockType="command"  
    //% NUM.shadow="number" NUM1.defl="0"
    export function RGBmove1(parameter: any, block: any) { 
        let num=parameter.NUM.code;
        Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
		Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P13)),3)`);
		Generator.addCode(`RGB.shift(${num})`)  
    }


    //% block="The RGB light rotates [NUM] unit" blockType="command"  
    //% NUM.shadow="number" NUM.defl="0"
    export function RGBmove2(parameter: any, block: any) { 
        let num=parameter.NUM.code;
        Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
		Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P13)),3)`);
		Generator.addCode(`RGB.rotate(${num})`)  
    }



    //% block="RED[RED] GREEN[GREEN] BLUE[BLUE]" blockType="reporter"  
    //% RED.shadow="range" RED.params.min=0 RED.params.max=255 RED.defl=255
    //% GREEN.shadow="range" GREEN.params.min=0 GREEN.params.max=255 GREEN.defl=255
    //% BLUE.shadow="range" BLUE.params.min=0 BLUE.params.max=255 BLUE.defl=255
    export function color(parameter: any, block: any) { 
        let red=parameter.RED.code;
        let green=parameter.GREEN.code;
        let blue=parameter.BLUE.code;
        Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
		Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P13)),3)`);
		Generator.addCode(`(${red},${green},${blue})`)   
    }


    //% block="电机" blockType="tag"
    export function tagtest3() {}


    //% block="Motor [MOTOR] [TURN] at [SPEED]" blockType="command"  
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR" 
    //% TURN.shadow="dropdown" TURN.options="TURN" 
    //% SPEED.shadow="range" SPEED.params.min=0 SPEED.params.max=1023 SPEED.defl=512
    export function MOTORrotation(parameter: any, block: any) { 
        let motor=parameter.MOTOR.code;
        let turn=parameter.TURN.code;
        let speed=parameter.SPEED.code;
		Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`, `Board().begin()`, { once: true });
        Generator.addInit(`init_SHIELD`, `shield = SHIELD()`, { once: true });
        if (motor == "M1"){

            Generator.addCode(`shield.m1.run(${turn},${speed})`);
        }
        else if (motor == "M2"){


            Generator.addCode(`shield.m2.run(${turn},${speed})`);
        }
        else{


            Generator.addCode(`shield.m1.run(${turn},${speed})`);
			Generator.addCode(`shield.m2.run(${turn},${speed})`);
        }       
    }


    //% block="Motor [MOTOR] stop" blockType="command"  
    //% MOTOR.shadow="dropdown" MOTOR.options="MOTOR" 
    export function MOTORstop(parameter: any, block: any) { 
        let motor=parameter.MOTOR.code;
	    Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
        if (motor == "M1"){

            Generator.addCode(`shield.m1.stop()`);
        }
        else if(motor == "M2"){

            Generator.addCode(`shield.m2.stop()`);
        }
        else{

            Generator.addCode(`shield.m1.stop()`);
            Generator.addCode(`shield.m2.stop()`);
        } 
    }


    //% block="舵机" blockType="tag"
    export function tagtest4() {}
    //% block="Servo Pin[servo] Angle[angle]" blockType="command"  
	//% servo.shadow="dropdown" servo.options="SERVO"
	//% angle.shadow="angle" angle.params.edge=1
    export function ServoWriteAngle(parameter: any, block: any) { 
        let servo=parameter.servo.code;
		let angle=parameter.angle.code;
	    Generator.addImport('from pinpong.board import Board,Pin,NeoPixel,IRRemote,IRRecv,Servo');
        Generator.addImport('from pinpong.extension.unihiker import *');
		Generator.addImport('from shield import SHIELD');
		Generator.addInit(`board_begin`,`Board().begin()`);
		Generator.addInit(`init_SHIELD`,`shield = SHIELD()`);
		Generator.addCode(`shield.${servo}.write_angle(${angle})`);
    }
}
